home *** CD-ROM | disk | FTP | other *** search
- /*****
- *
- * MemoryUtil.c
- *
- * This is a support file for "Grant's CGI Framework".
- * Please see the license agreement that accompanies the distribution package
- * for licensing details.
- *
- * Copyright ©1995,1996 by Grant Neufeld
- * grant@acm.com
- * http://arpp.carleton.ca/grant/
- *
- *****/
-
- #include "MyConfiguration.h"
-
- #include "compiler_stuff.h"
- #include "globals.h"
-
- #include "MemoryUtil.h"
-
-
- /*** LOCAL PROTOTYPES ***/
-
- pascal long memoryGrowZone ( Size );
-
-
- /*** MEMORY ALLOCATION ***/
-
- /* I use these 'wrappers' to toolbox memory calls to provide a way
- to consistantly handle error conditions. */
- p_export
- Handle
- MemoryNewHandle ( long theSize, OSErr *outErr )
- {
- Handle theHandle;
- OSErr theErr;
-
- theHandle = NewHandle ( theSize );
- theErr = MemError ();
-
- if ( outErr != NULL )
- {
- *outErr = theErr;
- }
-
- if ( theErr != noErr )
- {
- return NULL;
- }
- else
- {
- return theHandle;
- }
- }
-
- p_export
- Handle
- MemoryNewHandleClear ( long theSize, OSErr *outErr )
- {
- Handle theHandle;
- OSErr theErr;
-
- theHandle = NewHandleClear ( theSize );
- theErr = MemError ();
-
- if ( outErr != NULL )
- {
- *outErr = theErr;
- }
-
- if ( theErr != noErr )
- {
- return NULL;
- }
- else
- {
- return theHandle;
- }
- }
-
- p_export
- Ptr
- MemoryNewPtr ( long theSize, OSErr *outErr )
- {
- Ptr thePtr;
- OSErr theErr;
-
- thePtr = NewPtr ( theSize );
- theErr = MemError ();
-
- if ( outErr != NULL )
- {
- *outErr = theErr;
- }
-
- if ( theErr != noErr )
- {
- return NULL;
- }
- else
- {
- return thePtr;
- }
- }
-
- p_export
- Ptr
- MemoryNewPtrClear ( long theSize, OSErr *outErr )
- {
- Ptr thePtr;
- OSErr theErr;
-
- thePtr = NewPtrClear ( theSize );
- theErr = MemError ();
-
- if ( outErr != NULL )
- {
- *outErr = theErr;
- }
-
- if ( theErr != noErr )
- {
- return NULL;
- }
- else
- {
- return thePtr;
- }
- }
-
-
- /*** EMERGENCY MEMORY ***/
- #pragma mark -
-
- /* IM-Memory: 1-46 */
- Boolean
- InitializeEmergencyMemory ( OSErr *outErr )
- {
- Boolean success;
- OSErr theErr;
-
- success = true;
-
- gEmergencyMemory = NewHandle ( kMemCushionSize );
- theErr = MemError ();
-
- if ( outErr != NULL )
- {
- *outErr = theErr;
- }
-
- if ( theErr != noErr )
- {
- gEmergencyMemory = NULL;
- success = false;
- }
- else
- {
- SetGrowZone ( NewGrowZoneProc(memoryGrowZone) );
- }
-
- return success;
- } /* InitializeEmergencyMemory */
-
-
- /* IM-Memory: 1-48 */
- pascal long
- memoryGrowZone ( Size cbNeeded )
- {
- OSErr theErr;
- long theA5;
- long theSize;
-
- theA5 = SetCurrentA5 ();
-
- if ( *gEmergencyMemory && ( gEmergencyMemory != GZSaveHnd() ) )
- {
- EmptyHandle ( gEmergencyMemory );
-
- theErr = MemError ();
-
- if ( theErr != noErr )
- {
- theSize = nil;
- }
- else
- {
- theSize = kMemCushionSize; /* kEmergencyMemorySize in IM */
- }
- }
- else
- {
- theSize = nil;
- }
-
- theA5 = SetA5 ( theA5 );
-
- return theSize;
- } /* memoryGrowZone */
-
-
- /* To check that the memory reserve is intact.
- Returns true if there is emergency memory on reserve.
- IM-Memory: 1-47 */
- Boolean
- IsEmergencyMemAvail ( void )
- {
- return ( (gEmergencyMemory != NULL) && (*gEmergencyMemory != NULL) );
- } /* IsEmergencyMemAvail */
-
-
- /* IM-Memory: 1-48 */
- #if !(kCompileWithQuitOnLowMemory)
- OSErr
- RecoverEmergencyMemory ( void )
- {
- ReallocateHandle ( gEmergencyMemory, kMemCushionSize );
-
- return MemError ();
- } /* RecoverEmergencyMemory */
- #endif
-
- /*** EOF ***/